package com.lysj.admin.master.bussiness.company.company.service;

import cn.hutool.crypto.digest.BCrypt;
import com.lysj.admin.master.auth.domain.Role;
import com.lysj.admin.master.auth.service.RoleService;
import com.lysj.admin.master.bussiness.admin.domain.Agent;
import com.lysj.admin.master.auth.domain.User;
import com.lysj.admin.master.bussiness.admin.service.AgentService;
import com.lysj.admin.master.auth.service.UserService;
import com.lysj.admin.master.bussiness.company.company.domain.Company;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.bussiness.company.company.mapper.CompanyMapper;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author Created by drj on 2019-12-27 14:11
 * @description
 */
@Slf4j
@Service
@Transactional
public class CompanyService extends MyService<CompanyMapper, Company> {


    @Resource
    private AgentService agentService;

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;


    public void saveCompany(Company model, String relationId, String password) {

        Agent agent = agentService.getById(relationId);
        model.setAgentName(agent.getName());
        model.setAgentId(relationId);
        save(model);
        Role role = roleService.lambdaQuery().eq(Role::getType, CommonConstant.COMPANY_ISSUER).one();
        //创建对应登录用户信息
        User user = new User();
        user.setUsername(model.getPhone());
        user.setPassword(BCrypt.hashpw(password));
        user.setType(CommonConstant.COMPANY_ISSUER);
        user.setRelationId(model.getId());
        user.setRoleId(role.getId());
        userService.save(user);
    }


}
